package br.com.portalecbahia;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.portalecbahia.connectionservices.SynchronizeNews;
import br.com.portalecbahia.customize.NewsListAdapter;
import br.com.portalecbahia.customize.PullToRefreshListView;
import br.com.portalecbahia.customize.PullToRefreshListView.OnRefreshListener;
import br.com.portalecbahia.dao.NewsDAO;
import br.com.portalecbahia.listner.NewsListeners;
import br.com.portalecbahia.model.News;
import br.com.portalecbahia.util.PortalECBahiaUtil;

import com.google.analytics.tracking.android.EasyTracker;
import com.raptureinvenice.webimageview.image.WebImageView;

public class NewsActivity  extends ListActivity implements NewsListeners{
	
	private WebImageView mainImageIMG;
	private TextView titleMainTX;
	private Context context;
	private List<News> lNews;
	private ListAdapter adapterListNews;
	private SynchronizeNews taskNews;
	private News mainNews ;
	private ImageView updateIMG;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news);
        
        EasyTracker.getInstance().setContext(this);
        
        titleMainTX =(TextView) findViewById(R.id.titleMainTX);
        mainImageIMG =(WebImageView) findViewById(R.id.mainImageIMG);
        context = this;
        updateIMG = (ImageView) findViewById(R.id.updateIMG);
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
            	if(PortalECBahiaUtil.haveNetworkConnection(context)){
            		taskNews = new SynchronizeNews(context);
            		taskNews.execute();
            	}else{
        			Toast.makeText(context, getString(R.string.internetOff), Toast.LENGTH_LONG).show();
        			  ((PullToRefreshListView) getListView()).onRefreshComplete();
            	}
            }
        });

        listNews();
       

    }
    
    @Override
    public void onStart() {
      super.onStart();
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
      super.onStop();
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }

	@Override
	public void onSynchronizeNewsComplete(Boolean response) {
		if(response){
			listNews();
		}else{
			Toast.makeText(context, getString(R.string.serverProblem), Toast.LENGTH_LONG).show();
		}
		
	    ((PullToRefreshListView) getListView()).onRefreshComplete();
	    updateIMG.clearAnimation();
		
	}
	
	public void updateList(View view){
		if(PortalECBahiaUtil.haveNetworkConnection(context)){
    		taskNews = new SynchronizeNews(context);
    		taskNews.execute();
    		animateUpdate();
    	}else{
			Toast.makeText(context, getString(R.string.internetOff), Toast.LENGTH_LONG).show();
			  ((PullToRefreshListView) getListView()).onRefreshComplete();
    	}

	}

	private void animateUpdate() {
		int   currentRotation = 0;
		RotateAnimation anim = new RotateAnimation(currentRotation, (360*4),
		Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
		currentRotation = (currentRotation + 45) % 360;
		anim.setInterpolator(new LinearInterpolator());
		anim.setDuration(4000);
		anim.setRepeatMode(Animation.INFINITE);
		anim.setRepeatCount(Animation.INFINITE);
		anim.setFillEnabled(true);
		anim.setFillAfter(true);
		updateIMG.startAnimation(anim);
		
	}

	private void listNews() {
	   	lNews = new ArrayList<News>();
    	lNews.clear();
    	lNews = new NewsDAO(this).findAll();    	
    	
    
    	int sizeList = lNews.size();
    	if(lNews.size()<10){    		
    		for (int i = 0; i < PortalECBahiaUtil.minimumListSize-sizeList; i++) {
    			lNews.add(new News());
			}
    	}
        mainNews = lNews.get(0);
    	lNews.remove(0);
    	
        adapterListNews = (ListAdapter) new NewsListAdapter(this, lNews);
	    setListAdapter(adapterListNews);
	    
	    titleMainTX.setText(mainNews.getTitle());

	    WebImageView.setDiskCachingDefaultCacheTimeout(60 * 60* 24*30); // 1 month expiration
	    mainImageIMG.setImageWithURL(context, mainNews.getUrlImage());
	    
		
	}
	
	public void detailMain(View view){
		
		Intent intent = null;

		intent = new Intent(this, NewsDetailActivity.class);
		intent.putExtra("news", mainNews);

		startActivity(intent);
		return;
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) { 
		super.onListItemClick(l, v, position, id);
			
		Intent intent = null;
		
		News newSelect = (News) (adapterListNews.getItem(position-1));
		
		if(newSelect!=null && newSelect.getId()!=null){
			intent = new Intent(this, NewsDetailActivity.class);
			intent.putExtra("news", newSelect);

			startActivity(intent);
		}
		return;
	}
    
}
